Ext.onReady(function(){
	//注册二次密码验证vtype
	Ext.apply(Ext.form.VTypes,{
		password:function(val,field){
			if(field.confirmTo){
				var pwd=Ext.get(field.confirmTo);
				return (val==pwd.getValue());
			}
			return true;
		}
	});
	//添加用户
 	var win_user = new Ext.Window({
 	  id:'user_win_',
	  title:"添加用户",
      width: 450,
      height:400,
      layout: 'fit',
      plain:true,
      buttonAlign:'center',
      closable:false,
      resizable:false,
      items: new Ext.form.FormPanel({
    	  	frame:true,
    	  	id:'user_win_form_',
			labelWidth:100,
			labelAlign:"right",
			layout:"form",
			width:318,
			height:370,
			bodyStyle:'padding:15px;',
			defaults:{msgTarget:'under',width:200},
			items:[
					{
					xtype:"textfield",
					id:"LOGIN_NAME_",
					fieldLabel:"用户登陆名",
					blankText:'请输入用户登陆名',
					allowBlank:false
				},
				{
					xtype:"textfield",
					id:"REAL_NAME_",
					fieldLabel:"真实姓名",
					blankText:'请输入真实姓名',
					allowBlank:false
				},
				{
					xtype:"textfield",
					id:"EMAIL_",
					fieldLabel:"E-MAIL",
					vtypeText:'邮箱格式不正确',
					allowBlank:false,
					blankText:'请输入邮箱',
					vtype:'email'
				},
				{
					xtype:"textfield",
					id:"PASSWORD_",
					fieldLabel:"密码",
					blankText:'请输入密码',
					allowBlank:false,
					inputType: 'password'
				},
				{
					xtype:"textfield",
					id:"PASSWORD_REPEAT_",
					fieldLabel:"确认密码",
					blankText:'请输入确认密码',
					allowBlank:false,
					inputType: 'password',
					vtype:'password',
					vtypeText:'两次密码不一致',
					confirmTo:'PASSWORD_'
				},
				{
					xtype:"textfield",
					fieldLabel:"组织结构",
					readOnly:true,
					id:'ORG_ID_',
					allowBlank:false,
					blankText:'请选择所属组织结构',
					emptyText:'请选择所属组织结构'
				}
			],
			buttons:[
				{
					xtype:"button",
					text:"提  交",
					type:"submit",
					handler:function(){
						if(Ext.getCmp('LOGIN_NAME_').getValue().trim().toLowerCase()=='admin'){
							Ext.Msg.show({
							 	   width:390,
								   title:'出错了!',
								   msg: "禁止使用的用户名：admin",
								   buttons: Ext.Msg.OK,
								   icon: Ext.MessageBox.ERROR
							  });
							return;
						}
						if(!Ext.getCmp('PASSWORD_REPEAT_').validate() | !Ext.getCmp('LOGIN_NAME_').validate() | !Ext.getCmp('REAL_NAME_').validate() | !Ext.getCmp('EMAIL_').validate() | !Ext.getCmp('PASSWORD_').validate() | !Ext.getCmp('ORG_ID_').validate()){
							return;
						}
						if(Ext.getCmp('PASSWORD_REPEAT_').getValue() != Ext.getCmp('PASSWORD_').getValue()){
							return;
						}
						Ext.Ajax.request({
							url:getRootPath()+"/user/addUser.html",
							method:'post',
						 	success: function(response,options){
								if(response.responseText.trim()!=''){
							 		Ext.Msg.show({
									 	   width:390,
										   title:'出错了!',
										   msg: response.responseText,
										   buttons: Ext.Msg.OK,
										   icon: Ext.MessageBox.ERROR
									  });
							 		return;
								}
					 			Ext.getCmp('grid1').reload();
					 			Ext.getCmp("user_win_form_").form.reset();
					 			win_user.hide();
						 	},
						 	failure: function(res){
						 		Ext.Msg.show({
								 	   width:390,
									   title:'出错了!',
									   msg: res.responseText,
									   buttons: Ext.Msg.OK,
									   icon: Ext.MessageBox.ERROR
								  });
						 	},
						 	params:{
								login_name_:Ext.getCmp('LOGIN_NAME_').getValue().trim(),
								real_name_:Ext.getCmp('REAL_NAME_').getValue().trim(),
								email_:Ext.getCmp('EMAIL_').getValue().trim(),
								password_:Ext.getCmp('PASSWORD_').getValue().trim(),
								org_id_:Ext.getCmp('ORG_ID_').code.trim()
						 	}
						})
					}
				},{
					xtype:"button",
					text:"关  闭",
					handler:function(){
						Ext.getCmp("user_win_form_").form.reset();
						win_user.hide();
					}
				}]
		})
});
 	//添加角色
 	var win_role = new Ext.Window({
 	  id:'role_win_',
	  title:"添加角色",
      width: 350,
      height:200,
      layout: 'fit',
      plain:true,
      buttonAlign:'center',
      closable:false,
      resizable:false,
      items: new Ext.form.FormPanel({
    	  	frame:true,
    	  	id:'role_win_form_',
			labelWidth:100,
			labelAlign:"right",
			layout:"form",
			width:318,
			height:320,
			bodyStyle:'padding:15px;',
			defaults:{msgTarget:'under'},
			items:[
				{
					xtype:"textfield",
					id:"LABEL_",
					fieldLabel:"角色名称",
					blankText:'请输入角色名称',
					allowBlank:false
				},
				{
					xtype:"textfield",
					id:"ROLE_",
					fieldLabel:"角色代码",
					blankText:'请输入角色代码',
					allowBlank:false
				}
			],
			buttons:[
				{
					xtype:"button",
					text:"提  交",
					type:"submit",
					handler:function(){
						if(Ext.getCmp('ROLE_').getValue().trim().toLowerCase()=='role_user'){
							Ext.Msg.show({
							 	   width:390,
								   title:'出错了!',
								   msg: "禁止使用的的角色名：ROLE_USER",
								   buttons: Ext.Msg.OK,
								   icon: Ext.MessageBox.ERROR
							  });
							return;
						}
						if(!Ext.getCmp('LABEL_').validate() | !Ext.getCmp('ROLE_').validate()){
							return;
						}
						Ext.Ajax.request({
							url:getRootPath()+"/user/addRole.html",
							method:'post',
						 	success: function(response,options){
								if(response.responseText.trim()!=''){
							 		Ext.Msg.show({
									 	   width:390,
										   title:'出错了!',
										   msg: response.responseText,
										   buttons: Ext.Msg.OK,
										   icon: Ext.MessageBox.ERROR
									  });
							 		return;
								}
					 			Ext.getCmp('grid2').reload();
					 			Ext.getCmp("role_win_form_").form.reset();
					 			win_role.hide();
						 	},
						 	failure: function(res){
						 		Ext.Msg.show({
								 	   width:390,
									   title:'出错了!',
									   msg: res.responseText,
									   buttons: Ext.Msg.OK,
									   icon: Ext.MessageBox.ERROR
								  });
						 	},
						 	params:{
						 		label_:Ext.getCmp('LABEL_').getValue().trim(),
						 		role_:Ext.getCmp('ROLE_').getValue().trim()
						 	}
						})
					}
				},{
					xtype:"button",
					text:"关  闭",
					handler:function(){
						Ext.getCmp("role_win_form_").form.reset();
						win_role.hide();
					}
				}]
		})
});
	//修改密码
 	var win_user_pwd_ = new Ext.Window({
 	  id:'user_win_pwd_',
	  title:"修改密码",
      width: 450,
      height:300,
      layout: 'fit',
      plain:true,
      buttonAlign:'center',
      closable:false,
      resizable:false,
      items: new Ext.form.FormPanel({
    	  	frame:true,
    	  	id:'user_win_form_pwd_',
			labelWidth:100,
			labelAlign:"right",
			layout:"form",
			width:318,
			height:270,
			bodyStyle:'padding:15px;',
			defaults:{msgTarget:'under',width:200},
			items:[
		        {
		    	    xtype:"textfield",
		    	    id:"CURRENT_USER_",
		    	    fieldLabel:"登录名",
		    	    disabled:true
		        },
				{
					xtype:"textfield",
					id:"PASSWORD_OLD_",
					fieldLabel:"当前密码",
					blankText:'请输入当前密码',
					allowBlank:false,
					inputType:'password'
				},
				{
					xtype:"textfield",
					id:"PASSWORD_NEW_1",
					fieldLabel:"新密码",
					blankText:'请输入新密码',
					allowBlank:false,
					inputType: 'password'
				},
				{
					xtype:"textfield",
					id:"PASSWORD_NEW_2",
					fieldLabel:"确认密码",
					blankText:'请输入确认密码',
					allowBlank:false,
					inputType: 'password',
					vtype:'password',
					vtypeText:'两次密码不一致',
					confirmTo:'PASSWORD_NEW_1'
				}
			],
			buttons:[
				{
					xtype:"button",
					text:"提  交",
					type:"submit",
					handler:function(){
						if(!Ext.getCmp('PASSWORD_OLD_').validate() | !Ext.getCmp('PASSWORD_NEW_1').validate() | !Ext.getCmp('PASSWORD_NEW_2').validate()){
							return;
						}
						if(Ext.getCmp('PASSWORD_NEW_1').getValue() != Ext.getCmp('PASSWORD_NEW_2').getValue()){
							return;
						}
						Ext.Ajax.request({
							url:getRootPath()+"/user/updatePwdForUser.html",
							method:'post',
						 	success: function(response,options){
								if(response.responseText.trim()!=''){
							 		Ext.Msg.show({
								 	   width:390,
									   title:'出错了!',
									   msg: response.responseText,
									   buttons: Ext.Msg.OK,
									   icon: Ext.MessageBox.ERROR
									});
							 		return;
								}
								Ext.Msg.show({
									width:390,
									title:'提示',
									msg: '修改成功!',
									buttons: Ext.Msg.OK,
									icon: Ext.MessageBox.INFO
								});
					 			Ext.getCmp("user_win_form_pwd_").form.reset();
					 			win_user_pwd_.hide();
						 	},
						 	failure: function(res){
						 		Ext.Msg.show({
							 	   width:390,
								   title:'出错了!',
								   msg: res.responseText,
								   buttons: Ext.Msg.OK,
								   icon: Ext.MessageBox.ERROR
						 		});
						 	},
						 	params:{
						 		id_:Ext.getCmp('CURRENT_USER_').code,
						 		password_old_:Ext.getCmp('PASSWORD_OLD_').getValue().trim(),
						 		password_new_:Ext.getCmp('PASSWORD_NEW_1').getValue().trim()
						 	}
						})
					}
				},{
					xtype:"button",
					text:"关  闭",
					handler:function(){
						Ext.getCmp("user_win_form_pwd_").form.reset();
						win_user_pwd_.hide();
					}
				}]
		})
});
})